﻿@{
    ViewBag.Title = "编辑用户";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<script src="~/Assets/global/Plugins/jquery-1.11.0.min.js"></script>
@using DoKnow.CloudEdu.CMS.Host.Models;
@using DoKnow.CloudEdu.Common.Enums;
@model DoKnow.CloudEdu.DataContract.Model.Base.BSUserInfo
<!-- BEGIN PAGE HEADER-->
@Html.Partial("_PageHeader", new PageHeader() { Title = "编辑用户", Urls = new Dictionary<string, string>() { { "用户管理", "/BsUser/Index" }, { "编辑用户", "#" } } })
<!-- END PAGE HEADER -->

<div class="portlet box purple">
    <div class="portlet-title">
        <div class="caption">
            <i class="fa fa-cogs"></i>
        </div>
        <div class="tools">
            <a href="javascript:;" class="collapse"></a>
        </div>
        <div class="actions">
            <a class="btn green" href="@Url.Action("Index",new {Code=Request.QueryString["Code"],Mobile=@Request.QueryString["Mobile"]})"><i class="fa  fa-mail-reply">返回</i> </a>
        </div>
    </div>
    <div class="portlet-body form">
        <!-- BEGIN FORM-->
        <form id="bsUserEdit" action="@Url.Action("EditSave")" method="post" class="form-horizontal">
            <input type="hidden" id="ID" name="ID" value="@Model.ID" />
            <div class="form-body">
                <div class="form-group">
                    <label class="col-md-3 control-label">

                    </label>
                    <div class="col-md-4 control-label">
                        <span class="required" aria-required="true">
                            @ViewBag.ErrMsg
                        </span>

                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-3 control-label">
                        用户类型&nbsp;&nbsp;&nbsp;
                    </label>
                    <div class="col-md-4">
                        @Html.Hidden("oldType", Model.Category)
                        <select id="userType" name="Category" class="form-control form-filter input-sm"></select>
                    </div>
                </div>
                <div class="form-group">
                    @Html.Hidden("oldUserCode", Model.Code)
                    <label class="col-md-3 control-label">
                        用户编号<span class="required" aria-required="true">*</span>
                    </label>
                    <div class="col-md-4">
                        <input type="text" id="txtCode" name="Code" value="@Model.Code" class="form-control" readonly="readonly">
                        <span class="help-block"></span>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-3 control-label">
                        手机号<span class="required" aria-required="true">*</span>
                    </label>
                    <div class="col-md-4">
                        <input type="text" id="txtMobile" name="Mobile" value="@Model.Mobile" class="form-control" placeholder="请输入手机号">
                        <span class="help-block"></span>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-3 control-label">
                        真实姓名
                    </label>
                    <div class="col-md-4">
                        <input type="text" id="txtRealName" name="RealName" value="@Model.RealName" class="form-control" placeholder="请输入真实姓名">
                        <span class="help-block"></span>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-3 control-label">
                        昵称
                    </label>
                    <div class="col-md-4">
                        <input type="text" id="txtNickName" name="NickName" value="@Model.NickName" class="form-control" placeholder="请输入真实昵称">
                        <span class="help-block"></span>
                    </div>
                </div>
                <div class="form-group">
                        <label class="col-md-3 control-label">
                            状态
                        </label>
                        <div class="col-md-4">
                            @Html.DropDownList("Status", ViewBag.ComStatus as SelectList, new { @class = "form-control input-medium"})
                        </div>
                    </div>
                <div class="form-group">
                    <label class="col-md-3 control-label">
                        备注&nbsp;&nbsp;&nbsp;
                    </label>
                    <div class="col-md-4">
                        <textarea style="height: 100px;" id="txtRemark" name="Remark" class="form-control" placeholder="请输入备注">@Model.Remark</textarea>
                        <span class="help-block"></span>
                    </div>
                </div>
            </div>
            <div class="form-actions">
                <div class="row">
                    <div class="col-md-offset-3 col-md-9">
                        <button type="submit" class="btn red" id="sure">确定</button>
                        <a href="@Url.Action("Index", new { Code = Request.QueryString["Code"], Mobile = @Request.QueryString["Mobile"] })" class="btn default">取消</a>
                    </div>
                </div>
            </div>
        </form>
        <!-- END FORM-->
    </div>
</div>
@section scripts{
    <script src="~/Assets/global/Plugins/jquery-validation/jquery.validate.min.js"></script>
    <script src="~/Assets/global/Plugins/jquery-validation/additional-methods.min.js"></script>
    <script src="~/Assets/global/Plugins/jsTree/dist/jstree.min.js"></script>
    <script type="text/javascript">
        function pageReady() {
            //获取类型
            var oldType = $("#oldType").val();
            $.ajax({
                url: "/BsUser/GetUserCategroy",
                type: "post",
                data: '',
                success: function (data) {
                    $("#userType").append(BindTypeOption(data));
                    $("#userType").find("option[value='" + oldType + "']").attr("selected", "selected");
                }
            });
            var form1 = $('#bsUserEdit');
            var error1 = $('.alert-danger', form1);
            var success1 = $('.alert-success', form1);

            form1.validate({
                errorElement: 'span',
                errorClass: 'help-block help-block-error',
                focusInvalid: false,
                ignore: "",
                messages: {
                    Mobile: {
                        required: "手机必填",
                        byteMaxLength: "名称要小于50个字节(一个中文字算2个字节)"
                    },
                    Remark: {
                        byteMaxLength: "备注要小于100个字节(一个中文字算2个字节)"
                    }
                },
                rules:
                {
                    Mobile: {
                        required: true,
                        byteMaxLength: 15
                    },
                    txtRemark: {
                        byteMaxLength: 100
                    }
                },

                invalidHandler: function (event, validator) {
                    success1.hide();
                    error1.show();
                    Metronic.scrollTo(error1, -200);
                },

                highlight: function (element) {
                    $(element).closest('.form-group').addClass('has-error');
                },

                unhighlight: function (element) {
                    $(element).closest('.form-group').removeClass('has-error');
                },

                success: function (label) {
                    label.closest('.form-group').removeClass('has-error');
                },

                submitHandler: function (form) {
                    success1.show();
                    error1.hide();
                    form.submit();
                }
            });
        }
        //生成类型下拉
        function BindTypeOption(objArray) {
            var optionList = "";
            if (objArray != null) {
                for (var i = 0; i < objArray.length; i++) {
                    var obj = objArray[i];
                    optionList += "<option id=\"" + obj.Key + "\" value=\"" + obj.Value + "\">" + obj.Key + "</option>";
                }
            }
            return optionList;
        }
        function CheckExist(setValue) {
            if ($("#userType").val() == "@((int)EnumHelper.UserCategory.Employee)" && ($("#oldStaffID").val() != $("#FACTORY_STAFF_ID").val())) {
                var checkResult = true;
                $.ajax({
                    url: "/BsUser/CheckStaffID?ID=" + $("#FACTORY_STAFF_ID").val(),
                    type: "post",
                    dataType: "json",
                    async: false,
                    success: function (data) {
                        checkResult = data.IsSuccess;
                        if (checkResult == false) {
                            alert("该工厂员工ID不存在！");
                            return false;
                        } else {
                            if (setValue) {
                                $("#txtCode").val(data.Employee.CODE);
                                $("#txtName").val(data.Employee.NAME);
                                $("#txtMOBILE").val(data.Employee.MOBILE);
                            } else {
                                checkResult = confirm("该员工为：" + data.Employee.NAME + "(" + data.Employee.CODE + "),请确认关联？");
                            };
                        }
                    }
                });
                return checkResult;
            }
        }
        $("#sure").click(function () {
            var result = CheckExist(false);
            return result;
        });

        $("#userType").change(function () {

            //console.log($("#userType").val())
            if ($("#userType").val() != "@((int)EnumHelper.UserCategory.Employee)") {
                document.getElementById("divStaff").style.display = "none";
            }
            else {
                document.getElementById("divStaff").style.display = "";
            }
        });
        $("#FACTORY_STAFF_ID").change(function () {
            CheckExist(true);
        });
    </script>
}
